﻿
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>菜单管理</title>
    <script src="~/Scripts/jquery-easyui-1.4.4/jquery.min.js"></script>
    <script src="~/Scripts/jquery-easyui-1.4.4/jquery.easyui.min.js"></script>
    <script src="~/Scripts/jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script>
    <script src="~/Content/js/common.js"></script>
    <link href="~/Content/themes/icon.css" rel="stylesheet" />
    <link href="~/Scripts/jquery-easyui-1.4.4/themes/bootstrap/easyui.css" rel="stylesheet" />
    <link href="~/Content/themes/table.css" rel="stylesheet" />
    <script type="text/javascript">
        $(function () {
            $.ajax({
                //请求当前用户可以操作的按钮
                url: "/Button/GetUserAuthorizeButton?r=" + Math.random(),
                type: "post",
                data: { "KeyCode": "menu", "KeyName": "Menu" },
                dataType: "json",
                timeout: 5000,
                success: function (data) {
                    if (data.success) {
                        var toolbar = getToolBar(data); //common.js
                        // 判断是否有查询权限
                        if (data.search) {
                            $("#ui_menu_dg").datagrid({
                                url: "/Menu/GetAllMenuInfo",
                                striped: true,     // 是否显示斑马线效果（即各行变色）
                                pagination: true,
                                pageSize: 20,
                                rownumbers: true,
                                singleSelect: true, // 单选
                                multiSort: true,    // 允许多列排序
                                idField: 'Id',     // 标识字段，用于选择行
                                sortName: 'ParentId,Sort', // 排序字段
                                sortOrder: 'asc,asc',      // 排序顺序
                                pageList: [10, 20, 40, 60, 80, 100],
                                frozenColumns: [[  // 固定列
                                    { field: 'Id', title: "菜单Id", width: 60 },
                                    { field: 'Name', title: "菜单名称", width: 150 },
                                ]],
                                columns: [[
                                    { field: 'ParentId', title: "父节点Id", width: 80, sortable: true }, // 此列可排序
                                    { field: 'Code', title: "标识码", width: 120 },
                                    { field: 'LinkAddress', title: "链接地址", width: 200 },
                                    { field: 'Icon', title: "图标", width: 150 },
                                    { field: 'Sort', title: "排序号", width: 60, sortable: true },// 此列可排序
                                    { field: 'UpdateTime', title: "最后更新时间", width: 150 },
                                    { field: 'UpdateBy', title: "最后更新人", width: 60 },
                                ]],
                                toolbar: toolbar.length == 0 ? null : toolbar
                                // 例如：Object {text: "添加", iconCls: "icon-add", handler: "AddMenu()"}
                            });
                        }
                        else {
                            $.show_alert("提示", "无权限，请联系管理员！");
                        }
                    }
                    else {
                        $.show_alert("错误", data.result);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus == "timeout") {
                        $.show_alert("提示", "请求超时，请刷新当前页重试！");
                    }
                    else {
                        $.show_alert("错误", textStatus + "：" + errorThrown);
                    }
                }
            });
            // 回车搜索
            $("#ui_menu_search").find('input').on('keyup',
                function (e) {
                    if (e.keyCode == '13') {
                        searchdata();
                    }
                }
            );
        });

        // 搜索
        function searchdata() {
            $("#ui_menu_dg").datagrid('load', {
                // 搜索字段
                MenuName: $("#txtSearchMenuName").val()
            });
            // 清理所有选择，清理所有勾选的行
            $("#ui_menu_dg").datagrid('clearSelections').datagrid('clearChecked');
        }

        // 新增
        function AddMenu() {
            $("<div/>").dialog({
                id: "ui_menu_add_dialog",
                href: "/Menu/MenuAdd",
                title: "添加菜单",
                height: 300,
                width: 400,
                modal: true,
                buttons: [{
                    id: "ui_menu_add_btn",
                    text: '添 加',
                    handler: function () {
                        $("#MenuAddForm").form("submit", {
                            url: "/Menu/AddMenu",
                            onSubmit: function (param) {
                                param.MenuParentId = $("#treeMenuParentId").combotree("getValues").toString();
                                param.MenuName = $("#txtMenuName").val()
                                param.MenuCode = $("#txtMenuCode").val();
                                param.MenuIcon = $("#comboxMenuIconTree").combotree("getValues").toString();
                                param.MenuSort = $('#txtMenuSort').val();
                                param.MenuLinkAddress = $("#txtMenuLinkAddress").val();

                                if ($(this).form('validate')) {
                                    $('#ui_menu_edit_btn').linkbutton('disable');
                                    return true;
                                }
                                else {
                                    $('#ui_menu_add_btn').linkbutton('enable');
                                    return false;
                                }
                            },
                            success: function (data) {
                                console.log(data)
                                var dataJson = eval('(' + data + ')'); // 加上圆括号是迫使eval函数强制将括号内表达式转化为对象。
                                $.show_alert("提示", dataJson.msg);
                                if (dataJson.success) {
                                    $("#ui_menu_add_dialog").dialog('destroy'); // 添加成功则销毁添加页面
                                    $("#ui_menu_dg").datagrid("reload");
                                }
                                else {
                                    $('#ui_menu_add_btn').linkbutton('enable');

                                }
                            }
                        });
                    }
                },
                {
                    text: '取消',
                    handler: function () {
                        $("#ui_menu_add_dialog").dialog('destroy');
                    }
                }],
                onLoad: function () {
                    $("#txtMenuName").focus();
                },
                onClose: function () {
                    $("#ui_menu_add_dialog").dialog('destroy');
                }
            });
        }

        // 修改
        function EditMenu() {
            var row = $("#ui_menu_dg").datagrid('getChecked');
            if (row.length < 1) {
                $.show_alert("提示", "请选择修改项");
                return;
            }
            $("<div/>").dialog({
                id: "ui_menu_edit_dialog",
                href: "/Menu/MenuEdit",
                title: "修改菜单",
                height: 300,
                width: 400,
                modal: true,
                buttons: [{
                    id: "ui_menu_edit_btn",
                    text: '修 改',
                    handler: function () {
                        $("#MenuEditForm").form("submit", {
                            url: "/Menu/EditMenu",
                            onSubmit: function (param) {
                                param.MenuParentId = $("#treeMenuParentId").combotree("getValues").toString();
                                param.MenuName = $("#txtMenuName").val()
                                param.MenuCode = $("#txtMenuCode").val();
                                param.MenuIcon = $("#comboxMenuIconTree").combotree("getValues").toString();
                                param.MenuSort = $('#txtMenuSort').val();
                                param.MenuLinkAddress = $("#txtMenuLinkAddress").val();
                                param.id = $("#hidid").val();
                                param.originalName = $("#hidoriginalName").val();

                                if ($(this).form('validate')) {
                                    $('#ui_menu_add_btn').linkbutton('disable');
                                    return true;
                                }
                                else {
                                    $('#ui_menu_edit_btn').linkbutton('enable');
                                    return false;
                                }
                            },
                            success: function (data) {
                                alert(data)
                                console.log(data)
                                var dataJson = $.parseJSON(data);
                                $.show_alert("提示", dataJson.msg);
                                if (dataJson.success) {
                                    $("#ui_menu_edit_dialog").dialog('destroy'); // 添加成功则销毁添加页面
                                    $("#ui_menu_dg").datagrid("reload");
                                }
                                else {
                                    $('#ui_menu_edit_btn').linkbutton('enable');
                                }
                            }
                        });
                    }
                },
                {
                    text: '取消',
                    handler: function () {
                        $("#ui_menu_edit_dialog").dialog('destroy');
                    }
                }],
                onLoad: function () {
                    $("#hidid").val(row[0].Id);
                    $("#hidoriginalName").val(row[0].Name);
                    $("#txtMenuName").val(row[0].Name);
                    $("#txtMenuCode").val(row[0].Code);
                    $('#comboxMenuIconTree').combotree('setValue', row[0].Icon);
                    $('#txtMenuSort').numberspinner('setValue', row[0].Sort);
                    $("#txtMenuLinkAddress").val(row[0].LinkAddress);
                    if (row[0].ParentId != "0") {
                        $("#treeMenuParentId").combotree("setValue", row[0].ParentId);
                    }
                },
                onClose: function () {
                    $("#ui_menu_edit_dialog").dialog('destroy');
                }
            });
        }

        // 删除
        function DelMenu() {
            var row = $("#ui_menu_dg").datagrid('getChecked');
            if (row.length < 1) {
                $.show_alert("提示", "请选择删除项");
                return;
            }
            $.messager.confirm('提示', '确定删除选中行吗？',
                function (r) {
                    if (r) {
                        var MenuIDs = "";
                        $.each(row, function (i, row) {
                            MenuIDs += row.Id + ",";
                        });
                        MenuIDs = MenuIDs.substring(0, MenuIDs.length - 1);
                        $.ajax({
                            url: "/Menu/DelMenuByIDs",
                            data: {
                                IDs: MenuIDs
                            },
                            type: "POST",
                            dataType: "json",
                            success: function (data) {
                                if (data.success) {
                                    $("#ui_menu_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                }
                                $.show_alert("提示", data.msg);
                            }
                        });
                    }
                }
            );
        }

        //分配按钮
        function SetMenuButton() {
            var row = $("#ui_menu_dg").datagrid("getChecked");
            if (row.length < 1) {
                $.show_alert("提示", "请先勾选要分配按钮的菜单");
                return;
            }
            if (row[0].ParentId == 0 || row[0].ParentId == "") {
                $.show_alert("提示", "根节点无法分配按钮");
                $("#ui_menu_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }

            $("<div/>").dialog({
                id: "setmenubuttondialog",
                href: "/Menu/MenuButtonSet",
                title: "分配按钮",
                height: 200,
                width: 400,
                modal: true,
                onLoad: function () {
                    $("#hidid").val(row[0].Id);
                    $("#txtMenuBtnName").val(row[0].Name);
                    //获取此菜单配置的按钮 设置按钮 ui_menubutton_button 选中
                    $.ajax({
                        url: "/Menu/GetMenuButtonByMenuID?r=" + Math.random(),
                        data: { menuid: row[0].Id },
                        success: function (data) {
                            if (data != null && data != "") {
                                $('#comButtonTree').combotree('setValues', data.split(','));
                            }
                        }
                    });
                },
                buttons: [{
                    id: "btnmenubuttonsave",
                    text: '保 存',
                    handler: function () {
                        $("#SetMenuButtonForm").form("submit", {
                            url: "/Menu/SetMenuButton",
                            onSubmit: function (param) {
                                $('#btnmenubuttonsave').linkbutton('disable');
                                param.menuid = $("#hidid").val();
                                param.buttonids = $("#comButtonTree").combotree("getValues").toString();
                                if ($(this).form('validate')) {
                                    return true;
                                }
                                else {
                                    $('#btnmenubuttonsave').linkbutton('enable');   //恢复按钮
                                    return false;
                                }
                            },
                            success: function (data) {
                                var dataJson = eval('(' + data + ')');    //转成json格式
                                if (dataJson.success) {
                                    $("#setmenubuttondialog").dialog('destroy');  //销毁dialog对象
                                    $.show_alert("提示", dataJson.msg);
                                } else {
                                    $('#btnmenubuttonsave').linkbutton('enable');    //恢复按钮
                                    $.show_alert("提示", dataJson.msg);
                                }
                            }
                        });
                    }
                }],
                onClose: function () {
                    $("#setmenubuttondialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }

    </script>
</head>
<body>
    <div id="ui_menu_layout" class="easyui-layout" data-options="fit:true,border:false">
        <!--搜索条件栏-->
        <div data-options="region:'north',split:true,border:true,collapsed:false"
             title="搜索条件" style="height:72px;">
            <div id="ui_menu_search">
                <table class="tableForm" style="height:28px;">
                    <tr>
                        <th height="20px">菜单名</th>
                        <td>
                            <input name="txtSearchMenuName" id="txtSearchMenuName"
                                   class="easyui-validatebox textbox" style="width:170px;height:22px;" />
                        </td>
                        <td>
                            <a href="javascript:void(0);" class="easyui-linkbutton" plain="true"
                               iconcls="icon-search" onclick="searchdata();">
                                搜索
                            </a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <!--列表栏-->
        <div data-options="region:'center',border:false">
            <table id="ui_menu_dg" data-options="fit:true,border:false"></table>
        </div>
    </div>
</body>
</html>


